﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;

public partial class UI_jcxm : System.Web.UI.Page
{
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        basic.CheckAdmin(this);
        basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());

        if (!IsPostBack)
        {
            bindData();
             if (Request["action"] == "xiugai") switchtype();
        }

    }

    private void switchtype()
    {   
        
          txbcontent.Text = shujuku.GetValueByKey("content,id", "jcxm", "id=" + Request["id"].ToString() );
          txbtitle.Text = shujuku.GetValueByKey("title,id", "jcxm", "id=" + Request["id"].ToString());
          btnok.Text = "确定修改";
     }
    
    void bindData()
    {
        string GetDateSql = "select * from jcxm  order by id desc";
        ds = shujuku.GetDataSet(GetDateSql);
        gvList.DataSource = ds;
        gvList.DataBind();
        LblCurrentIndex.Text = (gvList.PageIndex + 1).ToString();
        LblPageCount.Text = gvList.PageCount.ToString();
        LblRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
        if (ds.Tables[0].Rows.Count == 0)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

            LblCurrentIndex.Visible = false;
            LblPageCount.Visible = false;
            LblRecordCount.Visible = false;
            Label1.Visible = false;
            Label1.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;

            //lbMessage.Text = "没有记录！";
        }
        else if (gvList.PageCount == 1)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }
        // 计算生成分页页码,分别为："首 页" "上一页" "下一页" "尾 页"
        btnFirst.CommandName = "1";
        btnPrev.CommandName = (gvList.PageIndex == 0 ? "1" : gvList.PageIndex.ToString());
        btnNext.CommandName = (gvList.PageCount == 1 ? gvList.PageCount.ToString() : (gvList.PageIndex + 2).ToString());
        btnLast.CommandName = gvList.PageCount.ToString();

    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        gvList.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        bindData();
        btnall.Text = "选择全部";
    }
    protected void gvList_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        if (gvList.PageCount > 1)
        {
            gvList.PageIndex = e.NewPageIndex;
            bindData();
        }
        else
        {
            LblCurrentIndex.Text = gvList.EditIndex.ToString();
        }
    }
    protected void btnExecute_Click(object sender, EventArgs e)
    {
        string ExecSql = "";
        int ids;
        string type = ddlOper.SelectedValue.ToString();

        if (type == "del") ExecSql = "delete from jcxm where id=";
        for (int i = 0; i < gvList.Rows.Count; i++)
        {
            if (((CheckBox)gvList.Rows[i].Cells[0].FindControl("chkContentId")).Checked)
            {
                ids = Convert.ToInt32(gvList.Rows[i].Cells[1].Text);
                string Resql = ExecSql + ids;
                if (shujuku.ExecuteSql(Resql))
                {
                    LogNet.LogBLL.info("用户:" + FTchina.Library.MasterHelper.MasterName + "删除了编号为" + ids + "的基础项目信息");
                }
            }
        }
        bindData();
        btnall.Text = "选择全部";
    }
    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < gvList.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#f4f5eb'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

    }

    protected void btnall_Click(object sender, EventArgs e)
    {
        if (btnall.Text == "选择全部")
        {
            foreach (GridViewRow row in gvList.Rows)
            {
                ((CheckBox)row.Cells[0].FindControl("chkContentId")).Checked = true;
            }
            btnall.Text = "取消全选";
        }
        else
        {
            foreach (GridViewRow row in gvList.Rows)
            {
                ((CheckBox)row.Cells[0].FindControl("chkContentId")).Checked = false;
            }
            btnall.Text = "选择全部";
        }

    }


    protected void btnok_Click(object sender, EventArgs e)
    {
        string title = basic.FilterSQL(txbtitle.Text);
        string content = basic.SafeRequest(txbcontent.Text);
        if (title.Length == 0 || content.Length == 0) { JS.Alert("请填写标题、内容"); return; }

        string Sql = null;
        if (btnok.Text == "确定修改")
        {
            Sql = "update jcxm set title='" + title + "',content='" + content + "' where id=" + Request["id"];
        }
        else
        {
            Sql = "insert into jcxm (title,content)values('" + title + "','" + content + "')";
        }
        UImsg.PageRender( shujuku.ExecuteSql(Sql));

          
    }
}
